package com.example.myys.ui.page.webPage

import android.graphics.Bitmap
import android.util.Log
import android.webkit.WebView
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.myys.webView.AccompanistWebViewClient
import com.example.myys.webView.WebView
import com.example.myys.webView.rememberWebViewNavigator
import com.example.myys.webView.rememberWebViewState

@Composable
fun WebIndex(url: String, onClose: () -> Unit = {}) {
    val state = rememberWebViewState(url = url)
    val navigator = rememberWebViewNavigator()

    val webClient = remember {
        object : AccompanistWebViewClient() {
            override fun onPageStarted(
                view: WebView,
                url: String?,
                favicon: Bitmap?
            ) {
                super.onPageStarted(view, url, favicon)
                Log.d("Accompanist WebView", "Page started loading for $url")
            }

            /*override fun onPageFinished(view: WebView, url: String?) {
                super.onPageFinished(view, url)
                view?.evaluateJavascript("(function(){return document.documentElement.outerHTML;})()") { html ->
                    // 在这里处理获取到的HTML内容
                    println(html)
                }
            }*/
        }
    }
    Box(
        modifier = Modifier.fillMaxSize()
    ) {
        WebView(
            state = state,
            modifier = Modifier
                .fillMaxSize(),
            navigator = navigator,
            onCreated = { webView ->
                webView.settings.javaScriptEnabled = true
                webView.settings.javaScriptCanOpenWindowsAutomatically = true
                webView.settings.domStorageEnabled = true
                webView.settings.loadsImagesAutomatically = true
            },
            client = webClient
        )
        Row(modifier = Modifier.align(Alignment.BottomCenter)) {
            Button(
                onClick = { onClose() },
                modifier = Modifier
                    .padding(8.dp)
            ) {
                Text("关闭")
            }
            Button(
                onClick = { navigator.navigateBack() },
                modifier = Modifier
                    .padding(8.dp),
            ) {
                Text(text = "回退")
            }
            Button(
                onClick = { navigator.reload() },
                modifier = Modifier
                    .padding(8.dp)
            ) {
                Text(text = "刷新")
            }
            Button(
                onClick = {
                    state.webView?.evaluateJavascript("(function(){return document.documentElement.outerHTML;})()") { html ->
                        // 在这里处理获取到的HTML内容
                        println(html)
                    }
                },
                modifier = Modifier
                    .padding(8.dp),
            ) {
                Text(text = "获取页面")
            }
        }
    }

    /*AndroidView(
        factory = { context ->
            val webView = WebView(context)
            webView.settings.javaScriptEnabled = true
            webView.settings.javaScriptCanOpenWindowsAutomatically = true
            webView.settings.domStorageEnabled = true
            webView.settings.loadsImagesAutomatically = true
            webView.settings.mediaPlaybackRequiresUserGesture = false
            webView.webViewClient =object :WebViewClient(){
                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
                    view?.evaluateJavascript("(function(){return document.documentElement.outerHTML;})()") { html ->
                        // 在这里处理获取到的HTML内容
                        println(html)
                    }
                }
            }
            webView.loadUrl(url)
            webView
        })*/
}


